package com.lf.web.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

import java.util.Properties;

/**
 * Created by LF on 2017/5/7.
 */
@Configuration
public class MySimpleMappingExceptionResolver {

    @Bean("simpleMappingExceptionResolver")
    public SimpleMappingExceptionResolver create() {
        SimpleMappingExceptionResolver simpleMappingExceptionResolver = new SimpleMappingExceptionResolver();
        //<!-- 定义默认的异常处理页面，当该异常类型的注册时使用 -->
        simpleMappingExceptionResolver.setDefaultErrorView("error");
        // <!-- 定义异常处理页面用来获取异常信息的变量名，默认名为exception -->
        simpleMappingExceptionResolver.setExceptionAttribute("ex");
        // <!-- 定义需要特殊处理的异常，用类名或完全路径名作为key，异常以页名作为值 -->
        Properties mappings = new Properties();
        mappings.setProperty("java.lang.Exception", "business_error");

        simpleMappingExceptionResolver.setExceptionMappings(
                mappings
        );
        return simpleMappingExceptionResolver;
    }
}
